import uuid

from api.vo.ResponseVo import ResponseVo
from api.vo.form.llm_form import LLM
import api.repository.llm_repository as llm_repository
import api.repository.llm_factory_repository as llm_factory_repository
import api.model.llm_model as llm_model

def deletes(factory: str):
    return ResponseVo(data=llm_repository.delete_by_factory(factory=factory))

def list_group_by_model_type():
    result = {}
    llms = llm_repository.get_llm_by_model_type()
    for e in llms:
        if e.modelType in result:
            result[e.modelType].append(e)
        else:
            result[e.modelType] = [e]
    return ResponseVo(data=result)


def add(llm: LLM):
    llm = llm_model.LLM(id=str(uuid.uuid4()),name=llm.name,factory=llm.factory,model_type=llm.modelType,api_key=llm.apiKey,base_url=llm.baseUrl,secret_key=llm.secretKey,token=llm.token )
    return ResponseVo(data=llm_repository.add(llm=llm))

def exist_by_name(name: str):
    return ResponseVo(data=llm_repository.exist_by_name(name=name))

def delete(id: str):
    return ResponseVo(data=llm_repository.delete(id=id))

def list():
    result = {}
    list = llm_repository.list()
    for e in list:
        if e.factory in result:
            result[e.factory]["llms"].append(e)
        else:
            fac = llm_factory_repository.get_factory(e.factory)
            result[e.factory] = {
                "factory": fac,
                "llms": [e]
            }
    return ResponseVo(data=result)